package file1;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Demo6 {
    public static void main(String[] args) throws IOException {
        //通过文件路径打开一个文件
        InputStream inputStream=new FileInputStream("./test.txt");
        byte[]array=new byte[1024];
        //将文件中的数据全部读到数组中，返回读到的字节数
        int byteNumber=inputStream.read(array);
        System.out.println("读到了 "+byteNumber+" 个字节");
        for (int i = 0; i < array.length; i++) {
            if(array[i]!=0)
            System.out.print(array[i]+"  ");
        }
        System.out.println();

        //将读到的字节数据构造为字符串
        String s = new String(array, 0, byteNumber, "utf8");
        System.out.println(s);

        inputStream.close();
    }
}
